package com.duvee.uiautomator;

import java.io.File;
import java.io.IOException;

import android.os.Handler;
import android.os.RemoteException;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

//所有UI自动测试都要继承UiAutomatorTestCase  
public class MessageTestCase extends UiAutomatorTestCase {
	public void testDemo() {
		// 获取UiDevice对象
		UiDevice device = getUiDevice();
		// 唤醒屏幕
		try {
			device.wakeUp();
			assertTrue("screenOn: can't wakeup", device.isScreenOn());
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		// 点击home键操作
		device.pressHome();
		Handler handler;
		// 启动APP
		try {
			Runtime.getRuntime()
					.exec("am start -n com.shenzhou.lbt/com.shenzhou.lbt.activity.main.WelcomeActivity");
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 如截图中提到的，我们使用content-desc属性Apps选择所有应用列表
		UiSelector uiSel = new UiSelector()
				.resourceIdMatches("com.shenzhou.lbt:id/login_edtUsername");
		UiObject userName = new UiObject(uiSel);

		try {
			userName.clickBottomRight();
			userName.clearTextField();
			userName.clickAndWaitForNewWindow(1000);
			userName.setText("15093131283");
			uiSel = new UiSelector()
					.resourceIdMatches("com.shenzhou.lbt:id/login_edtPassword");
			userName = new UiObject(uiSel);
			userName.clearTextField();
			userName.clickAndWaitForNewWindow(1000);
			userName.setText("6666666");
			userName = new UiObject(
					new UiSelector()
							.resourceIdMatches("com.shenzhou.lbt:id/login_btnEnter"));
			userName.clickAndWaitForNewWindow(5000);
			getUiDevice().takeScreenshot(new File(""), 0, 50);
			userName = new UiObject(new UiSelector()
					.resourceIdMatches("com.shenzhou.lbt:id/tv_list_item_name")
					.text("乐贝通幼儿园").className("android.widget.TextView"));
			userName.click();
			userName = new UiObject(new UiSelector()
					.resourceIdMatches("com.shenzhou.lbt:id/tv_list_item_name")
					.text("园长").className("android.widget.TextView"));
			userName.click();
			userName = new UiObject(new UiSelector()
					.resourceIdMatches(
							"com.shenzhou.lbt:id/common_title_level_one_btn")
					.text("确定").className("android.widget.Button"));
			userName.click();
			userName = new UiObject(new UiSelector()
					.resourceIdMatches("com.shenzhou.lbt:id/main_desc_dynamic")
					.text("圈子").className("android.widget.TextView"));
			userName.click();
			userName = new UiObject(new UiSelector().resourceIdMatches(
					"com.shenzhou.lbt:id/common_title_level_one_btn2")
					.className("android.widget.ImageView"));
			userName.click();
			userName = new UiObject(new UiSelector().text("照片"));
			userName.click();
			userName = new UiObject(new UiSelector().text("拍照"));
			userName.click();
			userName = new UiObject(
					new UiSelector()
							.resourceId("com.android.gallery3d:id/shutter_button"));
			userName.click();
			userName = new UiObject(
					new UiSelector()
							.resourceId("com.android.gallery3d:id/btn_done"));
			userName.click();
			userName = new UiObject(new UiSelector().resourceId(
					"com.shenzhou.lbt:id/club_sub_mybaby_config_type_value")
					.packageName("com.shenzhou.lbt"));
			userName.click();
			userName = new UiObject(
					new UiSelector()
							.resourceId(
									"com.shenzhou.lbt:id/club_dialog_type_grid_item_btn")
							.packageName("com.shenzhou.lbt").text("群体活动"));
			userName.click();
			userName = new UiObject(new UiSelector()
					.resourceId("com.shenzhou.lbt:id/common_title_btnSure")
					.packageName("com.shenzhou.lbt").text("确定"));
			userName.click();
			userName = new UiObject(new UiSelector()
					.resourceId("com.shenzhou.lbt:id/common_title_btnSure")
					.packageName("com.shenzhou.lbt").text("确定"));
			userName.clickAndWaitForNewWindow();
			userName = new UiObject(new UiSelector().resourceId(
					"com.shenzhou.lbt:id/common_title_left_img").packageName(
					"com.shenzhou.lbt"));
			userName.clickAndWaitForNewWindow();
			userName = new UiObject(new UiSelector()
					.resourceIdMatches("com.shenzhou.lbt:id/main_desc_mine")
					.text("我的").className("android.widget.TextView"));
			userName.click();
			userName = new UiObject(new UiSelector().resourceIdMatches(
					"com.shenzhou.lbt:id/fm_main_mine_set").className(
					"android.widget.ImageView"));
			userName.click();
			userName = new UiObject(new UiSelector().resourceIdMatches(
					"com.shenzhou.lbt:id/sub_config_reload").className(
					"android.widget.Button"));
			userName.click();
			userName = new UiObject(new UiSelector().resourceIdMatches(
					"com.shenzhou.lbt:id/btnSure").className(
					"android.widget.Button"));
			userName.click();
		} catch (UiObjectNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("我成功了！");
	}
}